package model

import (
	"common/util"
	"errors"
	"fmt"
	g "github.com/doug-martin/goqu/v9"
)

type ConfigVipParams struct {
	Level       uint32  `json:"level" cbor:"level" db:"level" goqu:"level"`
	NeedRunning float64 `json:"needRunning" cbor:"needRunning" db:"needRunning" goqu:"needRunning"`
	UpBonus     float64 `json:"upBonus" cbor:"upBonus" db:"upBonus" goqu:"upBonus"`
	WeekBonus   float64 `json:"weekBonus" cbor:"weekBonus" db:"weekBonus" goqu:"weekBonus"`
	MonthBonus  float64 `json:"monthBonus" cbor:"monthBonus" db:"monthBonus" goqu:"monthBonus"`
}
type ConfigVip struct {
	ConfigVipParams
}
type ConfigVipPageParams struct {
	ConfigVip
	util.PageParams
	Data []ConfigVip `json:"data"`
}
type ConfigVipModel struct {
	TableName string
}

const configVipPrimaryKey = "level"

func (m ConfigVipModel) GetEntityToEx(params ConfigVip) g.Expression {
	ex := g.Ex{}
	if params.WeekBonus != 0 {
		ex["weekBonus"] = params.WeekBonus
	}
	if params.MonthBonus != 0 {
		ex["monthBonus"] = params.MonthBonus
	}
	if params.NeedRunning != 0 {
		ex["needRunning"] = params.NeedRunning
	}
	if params.UpBonus != 0 {
		ex["upBonus"] = params.UpBonus
	}
	if params.Level != 0 {
		ex["level"] = params.Level
	}
	return ex
}
func (m ConfigVipModel) Get(id uint32, ent interface{}) error {
	if id == 0 {
		return errors.New("参数错误")
	}
	ex := g.Ex{configVipPrimaryKey: id}
	return m.GetByEx(ex, ent)
}
func (m ConfigVipModel) GetByEx(ex g.Expression, ent interface{}) error {
	query, _, _ := dialect.From(m.TableName).Where(ex).Limit(1).ToSQL()
	fmt.Printf("query sql :{%s}\n", query)
	return meta.MerchantDB.Get(ent, query)
}
func (m ConfigVipModel) CountByEx(ex g.Expression) (uint32, error) {
	from := dialect.From(m.TableName).Select(g.COUNT(configVipPrimaryKey))
	if ex != nil {
		from = from.Where(ex)
	}
	query, _, err := from.ToSQL()
	var total int
	if err = meta.MerchantDB.Get(&total, query); err != nil {
		return 0, err
	}
	return uint32(total), nil
}
func (m ConfigVipModel) Count() (uint32, error) {
	return m.CountByEx(nil)
}
func (m ConfigVipModel) Page(page *ConfigVipPageParams) error {
	return m.PageByEx(page, m.GetEntityToEx(page.ConfigVip))
}
func (m ConfigVipModel) PageByEx(page *ConfigVipPageParams, ex g.Expression) error {
	total, err := m.CountByEx(ex)
	if err != nil {
		return err
	}
	page.Total = total
	currentPage := page.CurrentPage
	if currentPage < 1 {
		currentPage = 1
	}
	offset := (currentPage - 1) * page.PageSize
	if offset < 0 {
		offset = 0
	}
	pageSize := page.PageSize
	if pageSize <= 0 {
		pageSize = 10
	}
	form := dialect.From(m.TableName)
	if ex != nil {
		form = form.Where(ex)
	}
	query, _, _ := form.Offset(uint(offset)).Limit(uint(pageSize)).Order(g.C(configVipPrimaryKey).Asc()).ToSQL()
	fmt.Printf(" sql :{%s}\n", query)
	ent := make([]ConfigVip, 0)
	err = meta.MerchantDB.Select(&ent, query)
	if err != nil {
		return err
	}
	page.CurrentPage = currentPage
	page.PageSize = pageSize
	page.Data = ent
	return nil
}
func (m ConfigVipModel) DeleteById(id uint32) error {
	return m.DeleteByEx(g.Ex{configVipPrimaryKey: id})
}
func (m ConfigVipModel) DeleteByEx(ex g.Expression) error {
	if ex == nil {
		return errors.New("参数错误")
	}
	query, _, err := dialect.Delete(m.TableName).Where(ex).ToSQL()
	fmt.Printf("delete sql :{%s}\n", query)
	_, err = meta.MerchantDB.Exec(query)
	if err != nil {
		return err
	}
	return nil
}
func (m ConfigVipModel) Update(entity ConfigVip) error {
	query, _, err := dialect.Update(m.TableName).Set(&entity.ConfigVipParams).Where(g.Ex{configVipPrimaryKey: entity.Level}).ToSQL()
	if err != nil {
		return err
	}
	_, err = meta.MerchantDB.Exec(query)
	fmt.Printf("update sql :{%s}\n", query)
	if err != nil {
		return err
	}
	return nil
}
func (m ConfigVipModel) Insert(entity ConfigVipParams) error {
	query, _, err := dialect.Insert(m.TableName).Rows(&entity).ToSQL()
	if err != nil {
		return err
	}
	res, err := meta.MerchantDB.Exec(query)
	fmt.Printf("insert sql :{%s}\n", query)
	id, err := res.LastInsertId()
	if err != nil {
		return err
	}
	fmt.Printf("insert sql :{%d}\n", id)
	return nil
}
func (m ConfigVipModel) Exists(id uint32) (bool, error) {
	cv := ConfigVip{}
	err := m.Get(id, &cv)
	if err == nil || cv.Level > 0 {
		return true, nil
	}
	return false, nil
}
